<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const obj = {
      uname: 'zs',
      age: 18,
      hobby: ['sing', 'dance', 'basketball'],
      family: {
        ma: 'zsma',
        ba: 'zsba'
      }
    }
    newObj = {}
    function deepClone(newObj, oldObj) {
      for (let key in oldObj) {
        // 如果用 instanceof 判断数据类型，一定先判断是否为数组再else if去判断对象，因为万物皆对象
        if (oldObj[key] instanceof Array) {
          newObj[key] = []
          deepClone(newObj[key], oldObj[key])
        }
        else if (oldObj[key] instanceof Object) {
          newObj[key] = {}
          deepClone(newObj[key], oldObj[key])
        }
        else {
          newObj[key] = oldObj[key]  //如果是数组，此时的key为索引；如果是对象，此时的key为属性名·
        }
      }
    }
    deepClone(newObj, obj)
    newObj.hobby[1] = '哈哈哈'
    console.log(newObj)
    console.log(obj)
  </script>
</body>

</html>